package com.study.leetcode;

//https://leetcode-cn.com/problems/daily-temperatures/submissions/
public class DailyTemperatures {
    public int[] dailyTemperatures1(int[] temperatures) {
        int length = temperatures.length;
        for (int i = 0; i < length; i++) {
            for (int j = i + 1; j < length; j++) {
                if (temperatures[j] > temperatures[i]) {
                    temperatures[i] = j - i;
                    break;
                }
                if (j == length - 1) {
                    temperatures[i] = 0;
                }
            }
        }
        temperatures[length - 1] = 0;
        return temperatures;
    }

    public int[] dailyTemperatures(int[] temperatures) {
        //新建一个结果集合用于存放结果
        int[] res = new int[temperatures.length];
        //遍历每一个数值
        for (int i = 0; i < temperatures.length; i++) {
            //查找当前 i 的后面的数值是否有比当前值大的数字
            for (int j = i + 1; j < temperatures.length; j++) {
                //如果有则将位置的差记录下来就是等待的天数,已经找到第一个大于的位置结束当前循环
                if (temperatures[j] > temperatures[i]) {
                    res[i] = j - i;
                    break;
                }
            }
        }
        return res;
    }
}
